package com.example.demo.zaqizabatest;

import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @ClassName Worker
 * @Author l42142
 * @Date 2021/3/3 9:54
 * @Description TODO
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaAvoidManuallyCreateThread")
public class Worker {
    private volatile boolean done;

    public void setDone(boolean done) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.done = done;
    }

    public void work() {
        long l = System.currentTimeMillis();
        while (!done) {
            //执行任务；
            System.out.println(done);
        }
        System.out.println(System.currentTimeMillis() - l);
    }

    public static void main(String[] args) {
        Worker worker = new Worker();
        new Thread(() -> worker.setDone(true)).start();
        worker.work();

        AbstractQueuedLongSynchronizer abstractQueuedLongSynchronizer = new AbstractQueuedLongSynchronizer() {
            @Override
            protected boolean tryAcquire(long arg) {
                return super.tryAcquire(arg);
            }

            @Override
            protected boolean tryRelease(long arg) {
                return super.tryRelease(arg);
            }

            @Override
            protected long tryAcquireShared(long arg) {
                return super.tryAcquireShared(arg);
            }

            @Override
            protected boolean tryReleaseShared(long arg) {
                return super.tryReleaseShared(arg);
            }

            @Override
            protected boolean isHeldExclusively() {
                return super.isHeldExclusively();
            }

            @Override
            public String toString() {
                return super.toString();
            }
        };

        AbstractQueuedSynchronizer abstractQueuedSynchronizer = new AbstractQueuedSynchronizer() {
            @Override
            protected boolean tryAcquire(int arg) {
                return super.tryAcquire(arg);
            }

            @Override
            protected boolean tryRelease(int arg) {
                return super.tryRelease(arg);
            }

            @Override
            protected int tryAcquireShared(int arg) {
                return super.tryAcquireShared(arg);
            }

            @Override
            protected boolean tryReleaseShared(int arg) {
                return super.tryReleaseShared(arg);
            }

            @Override
            protected boolean isHeldExclusively() {
                return super.isHeldExclusively();
            }

            @Override
            public String toString() {
                return super.toString();
            }
        };
    }
}
